<template>
    <el-dialog ref="dialogform" :title="addEditName + title" :visible.sync="dialogVisible" width="50%"
        :before-close="handleClose">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="120px" class="demo-ruleForm">
            <el-form-item label="用户名称" prop="userName">
                <el-input v-model="ruleForm.userName" class="w100p" clearable placeholder="请输入"></el-input>
            </el-form-item>
            <el-form-item label="姓名" prop="realName">
                <el-input v-model="ruleForm.realName" class="w100p" clearable placeholder="请输入"></el-input>
            </el-form-item>
            <el-form-item label="手机号" prop="phone">
                <el-input type="number" v-model="ruleForm.phone" class="w100p" clearable placeholder="请输入"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="password">
                <el-input type="password" v-model="ruleForm.password" class="w100p" clearable
                    placeholder="请输入"></el-input>
            </el-form-item>
            <el-form-item label="确认密码" prop="againPassword">
                <el-input v-model="ruleForm.againPassword" class="w100p" clearable placeholder="请输入"></el-input>
            </el-form-item>
            <el-form-item label="选择权限" prop="roleIdList">
                <el-select v-model="ruleForm.roleIdList" class="w100p" clearable filterable placeholder="请选择">
                    <el-option v-for="item, index in roleList" :key="index" :label="item.scopeTypeName"
                        :value="item.roleId"></el-option>
                </el-select>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="cancel">取 消</el-button>
            <el-button type="primary" @click="submitForm">确 定</el-button>
        </span>
    </el-dialog>
</template>
<script>
import userMessage from "@/api/userMessage"
export default {
    components: {},
    props: {
        addEditFlag: {
            type: String,
            default: () => {
                return 'add'
            }
        },
        data: {
            type: Object,
            default: () => {
                return {}
            }
        },
        title: {
            type: String,
            default: () => {
                return '操作表单'
            }
        },
        dialogVisible: {
            type: Boolean,
            default: () => {
                return false
            }
        },
        roleList: {
            type: Array,
            default: () => {
                return []
            }
        }
    },
    data() {
        let phoneNumber = (rule, phoneNumber, callback) => {
            const re = /^1[3456789]\d{9}$/;
            if (phoneNumber) {
                if (re.test(String(phoneNumber))) {
                    callback();
                } else {
                    return callback(new Error("请输入正确的手机号"));
                }
            } else {
                return callback(new Error("请输入手机号"));
            }
        };
        let password = (rule, value, callback) => {
            if (value == '') {
                callback(new Error('请输入新密码'));
            } else if (value.length < 6) {
                callback(new Error("密码不得小于六位"));
            } else {
                callback();
            }
        };
        let againPassword = (rule, value, callback) => {
            if (value == '') {
                callback(new Error('请再次输入密码'));
            } else if (value !== this.ruleForm.password) {
                callback(new Error('两次输入密码不一致!'));
            } else {
                callback();
            }
        };
        return {
            addEditName: '添加',
            addEditApi: '',
            ruleForm: {},
            rules: {
                userName: [
                    { required: true, message: '请输入', trigger: 'change' }
                ],
                realName: [
                    { required: true, message: '请输入', trigger: 'change' }
                ],
                phone: [
                    { required: true, validator: phoneNumber, trigger: 'change' }
                ],
                password: [
                    { required: false, validator: password, trigger: 'change' }
                ],
                againPassword: [
                    { required: false, validator: againPassword, trigger: 'change' }
                ],
            }
        }
    },
    computed: {

    },
    watch: {
        dialogVisible(v) {
            v && this.initData()
        }
    },

    mounted() { },
    methods: {
        initData() {
            if (this.addEditFlag == 'add') {
                this.resetForm()
                this.addEditApi = userMessage.add
                this.addEditName = '添加'
            } else if (this.addEditFlag == 'edit') {
                this.ruleForm = { ...this.data }
                this.addEditApi = userMessage.edit
                this.addEditName = '编辑'
            }
        },
        //提交表单
        submitForm() {
            this.$refs.ruleForm.validate((valid) => {
                if (valid) {
                    let params = { ...this.ruleForm }
                    for (let key in params) {
                        if (!params[key]) {
                            delete params[key]
                        }
                    }
                    this.addEditApi({ ...params }).then(res => {
                        let success = res.code == 0
                        let msg = success ? '操作成功' : '操作失败'
                        this.$message[success ? 'success' : 'warning'](res.msg || msg)
                        success && this.cancel();
                    }).catch(err => {
                        console.log(err)
                    })
                } else {
                    return false;
                }
            })
        },
        //重置
        resetForm() {
            this.ruleForm = {};
            this.$refs.ruleForm && this.$refs.ruleForm.resetFields()
        },
        //取消关闭
        cancel() {
            this.resetForm()
            this.$emit("closeDialog")
        },
        handleClose(done) {
            this.$confirm('确认关闭？')
                .then(_ => { this.resetForm(); this.cancel() })
                .catch(_ => { });
        }
    }
};
</script>
<style lang="scss" scoped></style>